from django import forms
from main.models import Product, Category, Order, Address, ProductImage
from django.contrib.auth import get_user_model


class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ('name','img','price', 'cost_price','stock','category','description')
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control'
            }),
            'price': forms.NumberInput(attrs={
                'class': 'form-control'
            }),
            'cost_price': forms.NumberInput(attrs={
                'class': 'form-control'
            }),
            'stock': forms.NumberInput(attrs={
                'class': 'form-control'
            }),
            'category': forms.Select(attrs={
                'class': 'form-control'
            }),
            'description': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 4
            })
        }


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        fields = ('title', 'parent', 'is_active')
        widgets = {
            'title': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入分类名称'
            }),
            'parent': forms.Select(attrs={
                'class': 'form-control'
            }),
            'is_active': forms.CheckboxInput(attrs={
                'class': 'form-check-input'
            })
        }


class OrderStatusForm(forms.ModelForm):
    """订单状态更新表单"""
    class Meta:
        model = Order
        fields = ('status',)
        widgets = {
            'status': forms.Select(attrs={
                'class': 'form-control'
            })
        }


class UserForm(forms.ModelForm):
    """用户表单"""
    class Meta:
        model = get_user_model()
        fields = ('username', 'email', 'is_active', 'is_staff')
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
        }


class AddressForm(forms.ModelForm):
    """地址表单"""
    class Meta:
        model = Address
        fields = ('user', 'recipient_name', 'phone', 'province', 'city', 'district', 'street', 'is_default')
        widgets = {
            'user': forms.Select(attrs={'class': 'form-control'}),
            'recipient_name': forms.TextInput(attrs={'class': 'form-control'}),
            'phone': forms.TextInput(attrs={'class': 'form-control'}),
            'province': forms.TextInput(attrs={'class': 'form-control'}),
            'city': forms.TextInput(attrs={'class': 'form-control'}),
            'district': forms.TextInput(attrs={'class': 'form-control'}),
            'street': forms.TextInput(attrs={'class': 'form-control'}),
        }


class ProductImageForm(forms.ModelForm):
    """商品图片表单"""
    class Meta:
        model = ProductImage
        fields = ['product', 'image', 'alt_text', 'is_main']
        widgets = {
            'product': forms.Select(attrs={'class': 'form-control'}),
            'image': forms.FileInput(attrs={'class': 'form-control'}),
            'alt_text': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '图片描述'}),
            'is_main': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
        }
        labels = {
            'product': '选择商品',
            'image': '图片',
            'alt_text': '图片描述',
            'is_main': '设为主图',
        }


class OrderForm(forms.ModelForm):
    """订单表单"""
    class Meta:
        model = Order
        fields = ('user', 'product', 'price', 'quantity', 'status', 'shipping_address')
        widgets = {
            'user': forms.Select(attrs={'class': 'form-control'}),
            'product': forms.Select(attrs={'class': 'form-control'}),
            'price': forms.NumberInput(attrs={'class': 'form-control'}),
            'quantity': forms.NumberInput(attrs={'class': 'form-control'}),
            'status': forms.Select(attrs={'class': 'form-control'}),
            'shipping_address': forms.Select(attrs={'class': 'form-control'}),
        }